// ListManager.h: interface for the CListManager class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_LISTMANAGER_H__AF521644_7CB1_4B64_95A2_94578A4177A5__INCLUDED_)
#define AFX_LISTMANAGER_H__AF521644_7CB1_4B64_95A2_94578A4177A5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "KtCommon/mutex.h"

template <class T>
class CListManager : public list<T>, public TCriticalSection
{
	//typedef list<T>::iterator		listIterator;
	typedef typename type_list_iterator(T)	listIterator;
	typedef list<T>							baselist;
	
public:
	CListManager();
	virtual ~CListManager();

public:
	int GetSize();
	BOOL IsEmpty();
	void PushFront(T x);
	T PopFront();
	void PushBack(T x);
	T PopBack();
	void Delete(T x);
	BOOL DelItem(T x);
	BOOL FindItem(T x);
	BOOL AddItem(T x);
	void ClearAll();
	int GetFront(T& x);
};

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
template <class T>
CListManager<T>::CListManager()
{
}

template <class T>
CListManager<T>::~CListManager()
{
	baselist::clear();
}

template <class T>
void CListManager<T>::PushFront(T x)
{
	Lock();
	baselist::push_front(x);
	Unlock();
}

template <class T>
void CListManager<T>::PushBack(T x)
{
	Lock();
	baselist::push_back(x);
	Unlock();
}

template <class T>
T CListManager<T>::PopFront()
{
	T RetV = NULL;

	Lock();
	listIterator listIter = baselist::begin();
	if (listIter != baselist::end())
	{
		RetV = *listIter;
		baselist::erase(listIter);
	}
	Unlock();	

	return RetV;
}

template <class T>
int CListManager<T>::GetFront(T& x)
{
	int iRetv = 0;
	Lock();
	listIterator listIter = baselist::begin();
	if (listIter != baselist::end())
	{
		x = *listIter;
		baselist::erase(listIter);
		iRetv = baselist::size();
	}
	else
		iRetv = 0;
	Unlock();

	return iRetv;
}


template <class T>
T CListManager<T>::PopBack()
{
	T RetV = NULL;

	Lock();
	if (!baselist::empty())
	{
		listIterator listIter = baselist::end();
		listIter--;
		RetV = *listIter;
		baselist::erase(listIter);
	}
	Unlock();	

	return RetV;
}

template <class T>
BOOL CListManager<T>::IsEmpty()
{
	BOOL RetV = FALSE;
	Lock();
	RetV = baselist::empty();
	Unlock();

	return RetV;
}

template <class T>
void CListManager<T>::Delete(T x)
{
	Lock();
	listIterator listIter = find(baselist::begin(), baselist::end(), x);
	if (listIter != baselist::end())
		baselist::erase(listIter);
	Unlock();
}

template <class T>
BOOL CListManager<T>::DelItem(T x)
{
	BOOL RetV = FALSE;
	Lock();
	listIterator listIter = find(baselist::begin(), baselist::end(), x);
	if (listIter != baselist::end())
	{
		baselist::erase(listIter);
		RetV = TRUE;
	}
	Unlock();		
	return RetV;
}


template <class T>
BOOL CListManager<T>::FindItem(T x)
{
	BOOL RetV = FALSE;
	Lock();
	listIterator listIter = find(baselist::begin(), baselist::end(), x);
	if (listIter != baselist::end())
		RetV = TRUE;
	Unlock();

	return RetV;
}

template <class T>
BOOL CListManager<T>::AddItem(T x)
{
	BOOL RetV = TRUE;
	Lock();
	listIterator listIter = find(baselist::begin(), baselist::end(), x);
	if (listIter != baselist::end())
		RetV = FALSE;
	else
		baselist::push_back(x);
	Unlock();	

	return RetV;
}

template <class T>
int CListManager<T>::GetSize()
{
	Lock();
	int RetV = baselist::size();
	Unlock();

	return RetV;
}


template <class T>
void CListManager<T>::ClearAll()
{
	Lock();
	baselist::clear();
	Unlock();
}


#endif // !defined(AFX_LISTMANAGER_H__AF521644_7CB1_4B64_95A2_94578A4177A5__INCLUDED_)
